<?php
/**
 * Checks whether there are still unparsed results in the results array
 *
 * @return bool
 * @package Concerto
 * @subpackage Query Functions
 */
function hasResults() {
	global $concerto;
	return $concerto->ContentQuery->getCurrentResultPointer();
}

/**
 * Initializes the current result in the loop
 *
 * @package Concerto
 * @subpackage Query Functions
 */
function initResult() {
	global $concerto;
	$concerto->ContentQuery->initResult();
}

/**
 * Initializes a query with the given parameters
 *
 * @param string $params An http-query string of parameters
 * @see \application\initializers\ContentQuery::initQuery()
 * @package Concerto
 * @subpackage Query Functions
 */
function initQuery($params) {
	global $concerto;
	$concerto->ContentQuery->initQuery($params);
}

/**
 * Ends the current query and removes it from the heap
 * 
 * @package Concerto
 * @subpackage Query Functions
 * @see \application\initializers\ContentQuery::endQuery()
 */
function endQuery() {
	global $concerto;
	$concerto->ContentQuery->endQuery();
}

/**
 * Ends the current users query and removes it from the heap
 * 
 * @package Concerto
 * @subpackage Query Functions
 * @see \application\initializers\UsersQuery::endQuery()
 */
function endUsersQuery() {
	global $concerto;
	$concerto->UsersQuery->endQuery();
}

/**
 * Display a field on a content
 *
 * @param string $field The exact display name of the field
 * @param array $params Optional array of parameters to pass to the prepareData function of the data type
 * @param int $queriesBack Optionally display a field from a different query
 * @package Concerto
 * @subpackage Query Functions
 * @see \application\datatypes\DataType::prepareData()
 */
function displayField($field, $params = null, $queriesBack = null) {
	global $concerto;
	echo $concerto->ContentQuery->getCurrentResult($queriesBack)->prepareData($field, $params);
}

/**
 * Return a field on a content
 *
 * @param string $field The exact display name of the field
 * @param array $params Optional array of parameters to pass to the prepareData function of the data type
 * @param int $queriesBack Optionally display a field from a different query
 * @return mixed The value returned by the data type's prepareData function
 * @package Concerto
 * @subpackage Query Functions
 * @see \application\datatypes\DataType::prepareData()
 */ 
function getField($field, $params = null, $queriesBack = null) {
	global $concerto;
	return $concerto->ContentQuery->getCurrentResult($queriesBack)->prepareData($field, $params);
}

/**
 * Get the value of a database cell on the current content
 *
 * @param string $field The column name
 * @return mixed The value in that column
 * @package Concerto
 * @subpackage Query Functions
 */
function getDBField($field) {
	global $concerto;
	return $concerto->ContentQuery->getCurrentResult()->getDBField($field);
}

/**
 * Checks if the current content has any feedback
 *
 * @return bool
 * @package Concerto
 * @subpackage Query Functions
 */
function hasFeedback() {
	global $concerto;
	return $concerto->ContentQuery->getCurrentResult()->hasFeedback();
}

/**
 * Initializes the current feedback for display
 * 
 * @package Concerto
 * @subpackage Query Functions
 */
function initFeedback() {
	global $concerto;
	$concerto->ContentQuery->getCurrentResult()->initFeedback();
}

/**
 * Return the rating associated with the current feedback
 * 
 * @package Concerto
 * @subpackage Query Functions
 * @return int The rating
 */
function feedbackRating() {
	global $concerto;
	return $concerto->ContentQuery->getCurrentResult()->feedbackRating();
}

/**
 * Return the comment associated with the current feedback
 *
 * @package Concerto
 * @subpackage Query Functions
 * @return string The comment
 */
function feedbackComment() {
	global $concerto;
	return $concerto->ContentQuery->getCurrentResult()->feedbackComment();
}

/**
 * Return the time a feedback was submitted
 * 
 * @param string $format The format that is passed to PHP's date function
 * @return string The date
 * @package Concerto
 * @subpackage Query Functions
 */
function feedbackTime($format = 'n/j/Y g:ia') {
	global $concerto;
	return $concerto->ContentQuery->getCurrentResult()->feedbackTime($format);
}

/**
 * Return the user that posted a feedback
 * 
 * @return mixed Either a \application\models\User object or just a string with the display name given, if they weren't logged in
 * @package Concerto
 * @subpackage Query Functions
 */
function feedbackUser() {
	global $concerto;
	return $concerto->ContentQuery->getCurrentResult()->feedbackUser();
}

/**
 * Checks if there are users in the users query
 *
 * @return bool
 * @package Concerto
 * @subpackage Template Functions
 */
function hasUsers() {
	global $concerto;
	return $concerto->UsersQuery->getCurrentResultPointer();
}

/**
 * Initializes the current user in the users query for display
 *
 * @package Concerto
 * @subpackage Template Functions
 */
function initUser() {
	global $concerto;
	$concerto->UsersQuery->initResult();
	
	
}

/**
 * Initialize a users query with the provided parameters
 *
 * @param string $query An http-query string of parameters
 * @see \application\initializers\UsersQuery::initQuery()
 * @package Concerto
 * @subpackage Template Functions
 */
function initUsersQuery($query) {
	global $concerto;
	$concerto->UsersQuery->initQuery($query);
}

/**
 * Display a field on the current user in the users query
 *
 * @param string $field The field name
 * @param arary $params An optional array of parameters for the field's data type's prepareData function
 * @package Concerto
 * @subpackage Query Functions
 * @see \application\datatypes\DataType::prepareData()
 */
function displayUserField($field, $params = null) {
	global $concerto;
	echo $concerto->UsersQuery->getCurrentResult()->prepareData($field, $params);
}

/**
 * Return a field on the current user in the users query
 *
 * @param string $field The field name
 * @param arary $params An optional array of parameters for the field's data type's prepareData function
 * @package Concerto
 * @subpackage Query Functions
 * @see \application\datatypes\DataType::prepareData()
 * @return mixed The data returned by the data type's prepareData function
 */
function getUserField($field, $params = null) {
	global $concerto;
	return $concerto->UsersQuery->getCurrentResult()->prepareData($field, $params);
}

/**
 * Get all contents with a certain relation with the current user
 *
 * For use in a users query only
 *
 * @param string $relation The name of the relation
 * @package Concerto
 * @subpackage Query Functions
 */
function queryRelations($relation) {
	$id = getField('ID');
	initQuery('content_relation='.$relation.'&content_relation_with='.$id);
}

/**
 * Query all children of the current content
 *
 * For use in a content query only
 *
 * @param int $type Optionally only query children of a certain content type
 * @package Concerto
 * @subpackage Query Functions
 */
function queryChildren($type = null) {
	$id = getField('ID');
	if($type) {
		initQuery('child_of='.$id.'&type='.$type);
	}
	else {
		initQuery('child_of='.$id);
	}
}

/**
 * Display page links for the current content query
 *
 * @see \application\initializers\ContentQuery::pageLinks()
 * @package Concerto
 * @subpackage Query Functions
 */
function pageLinks() {
	global $concerto;
	$concerto->ContentQuery->pageLinks();
}

/**
 * Initializes a query with aggregate data about content
 *
 * @param string $params Http-query string of parameters to pass to the buildQuery function
 * @param string $function The name of the aggregate function. Options are 'max', 'min', 'avg', 'median', 'mode'
 * @param string $column The name of the database column to get the aggregate data on
 * @package Concerto
 * @subpackage Query Functions
 */
function aggregateData($params, $function, $column) {
	global $concerto;
	if(!is_array($params)) {
		parse_str($params, $params);
	}
	
	switch($function) {
		case 'max':
		case 'maximum':
			$function = 'MAX';
			break;
			
		case 'min':
		case 'minimum':
			$function = 'MIN';
			break;
	
		case 'avg':
		case 'average':
		case 'mean':
			$function = 'AVG';
			break;
		
		case 'median':
			$function = 'MEDIAN';
			break;
		
		case 'mode':
			$function = 'MODE';
			break;
			
	}
	
	$function .= '('.$column.')';
	$params['aggregate'] = $function;
	$query = $concerto->ContentQuery->buildQuery($params);
	
	
	echo 'Query: '.$query;
	$results = DB()->getResults($query);
	echo '<pre>';
	print_r($results);
	echo '</pre>';
	
}

/**
 * Gets content with the same tags as the current content
 *
 * @param int $number The number of similar contents to display
 * @package Concerto
 * @subpackage Query Functions
 */
function relatedContent($number = 5) {
	global $concerto;
	// Get the name of the 'tags' field
	$type = getDBField('content_type');
	$id = getDBField('content_id');
	// Get Tags
	$tags = DB()->getVarArray("SELECT `tag_id` FROM `cn_tags` INNER JOIN `cn_content_tag_relations` ON `tag_id`=`ctr_tag` WHERE `ctr_content`='$id'");
	
	if($tags) {
		$tags = \application\utilities\Utility::sqlImplodedArray($tags);
		
		// Get names and permalinks of $number related contents
		$related = DB()->getResults("SELECT * FROM `cn_content` INNER JOIN `cn_content_tag_relations` ON `ctr_content`=`content_id` WHERE `content_type`='$type' AND `ctr_tag` IN ($tags) AND `content_id` != '$id' GROUP BY `content_id` ORDER BY RAND() LIMIT $number");
		if(count($related)) {
			echo '<ul class="related-content">';
			foreach($related as $content) {
				echo '<li><a href="'.\application\system\Permalink::buildContentPermalink($content->content_id).'">'.$content->content_title.'</a></li>';
			}
			echo '</ul>';
		}
	}
	
}





?>